package com.zrwl.poscloud.core.restapi.mobile;

import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.xo.mapper.DeptMapper;
import com.zrwl.poscloud.xo.mapper.EmployeeMapper;
import com.zrwl.poscloud.xo.service.UserService;
import com.zrwl.poscloud.xo.service.UserServiceOrderService;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import com.zrwl.poscloud.xo.vo.UserServiceOrderVO;
import com.zrwl.poscloud.xo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 服务采购 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-02-28
 */
@RestController
@Api(value = "服务采购相关接口", tags = {"服务采购相关接口"})
@RequestMapping("/serviceOrder")
@Slf4j
public class ServiceOrderRestApi {
    @Autowired
    private UserServiceOrderService userServiceOrderService;
    @Autowired
    private UserService userService;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private DeptMapper deptMapper;
    @AuthorityVerify
    @ApiOperation(value = "获取服务采购列表", notes = "获取服务采购列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody UserServiceOrderVO userServiceOrderVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取服务采购列表: {}", userServiceOrderVO);
        UserEmployeeRVO userEmployeeRVO = userService.getMe().getUserEmployeeRVO();
        userServiceOrderVO.setEmployeeUid(userEmployeeRVO.getEmployeeUid());
        return ResultUtil.successWithData(userServiceOrderService.getAllList(userServiceOrderVO));
    }
    @AuthorityVerify
    @OperationLogger(value = "新增服务采购")
    @ApiOperation(value = "新增服务采购", notes = "新增服务采购", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody UserServiceOrderVO userServiceOrderVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增服务采购: {}", userServiceOrderVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        userServiceOrderVO.setEmployeeUid(userEmployeeRVO.getEmployeeUid());
        userServiceOrderVO.setUserUid(userVO.getUid());
        EmployeeVO employeeVO = employeeMapper.getEmployeeByUid(userEmployeeRVO.getEmployeeUid());
        String CompanyUid = deptMapper.getCompanyUidByCompanyCode(employeeVO.getCompanyCode());
        userServiceOrderVO.setCode(userServiceOrderService.getAutoOrderCode(CompanyUid));
        return userServiceOrderService.add(userServiceOrderVO);
    }

    @AuthorityVerify
    @ApiOperation(value = "获取服务订单明细", notes = "获取服务订单明细", response = String.class)
    @PostMapping("/getServiceOrderDetailInfo")
    public String getServiceOrderDetailInfo(@Validated({GetList.class}) @RequestBody UserServiceOrderVO userServiceOrderVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取服务采购列表: {}", userServiceOrderVO);
        UserEmployeeRVO userEmployeeRVO = userService.getMe().getUserEmployeeRVO();
        userServiceOrderVO.setEmployeeUid(userEmployeeRVO.getEmployeeUid());
        return ResultUtil.successWithData(userServiceOrderService.getAllList(userServiceOrderVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取当前持卡人的购买状态", notes = "获取当前持卡人的购买状态", response = String.class)
    @PostMapping("/getUserOrderInfo")
    public String getUserOrderInfo(@Validated({GetList.class}) @RequestBody UserServiceOrderVO userServiceOrderVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取当前持卡人的购买状态: {}", userServiceOrderVO);
        UserEmployeeRVO userEmployeeRVO = userService.getMe().getUserEmployeeRVO();
        userServiceOrderVO.setEmployeeUid(userEmployeeRVO.getEmployeeUid());
        return ResultUtil.successWithData(userServiceOrderService.getUserOrderInfo(userServiceOrderVO));
    }
}
